**Code produced in 2015-03-13 by Allan Dafoe and Devin Caughey

clear
use "3-Analysis-Data/15-03-13-merging.dta"

** Indicator for Southern origin of Pres1 (under which MID started)
gen South1 = 0
replace South1 = 1 if inlist(Pres1, "Washington", "Jefferson", "Madison", "Monroe", "Jackson", "HarrisonWH", "Tyler", "Polk")
replace South1 = 1 if inlist(Pres1,  "Taylor", "JohnsonA", "Wilson", "Truman", "JohnsonLB", "Carter", "Clinton", "BushGW")
replace South1 = . if Pres1=="" 


** Indicator for Southern origin of Pres2 (who inherited the MID)
gen South2 = 0
order Pres2 South2
replace South2 = 1 if inlist(Pres2, "Washington", "Jefferson", "Madison", "Monroe", "Jackson", "HarrisonWH", "Tyler", "Polk")
replace South2 = 1 if inlist(Pres2, "Taylor", "JohnsonA", "Wilson", "Truman", "JohnsonLB", "Carter", "Clinton", "BushGW")
replace South2 = . if Pres2=="" 


** Start and end dates and length of presidents' terms
gen TermStartDate = mdy(TermStartMonth, TermStartDay, TermStartYear)
gen TermEndDate = mdy(TermEndMonth, TermEndDay, TermEndYear)  

** Assigning end of MID data as end of Term for Obama
replace TermEndDate = mdy(12, 31, 2010) if Pres1=="Obama"
replace TermStartDate = mdy(1, 1, 1816) if Pres1 == "Madison"

format TermEndDate TermStartDate %td
gen TermLength = TermEndDate - TermStartDate + 1 

** Make Cleveland's term length be the sum of his two terms.
tab TermLength if Pres1=="Cleveland1" | Pres1=="Cleveland2"
replace TermLength = 2* 1462 if Pres1=="Cleveland1" | Pres1=="Cleveland2"

order TermStartDate TermEndDate

*** Code to create variable proprec, which measures the proportion of the 
*** President's term that experienced a recession
** daysrec is Days in Recession
gen proprec = daysrec / TermLength

** Days into Pres1's term that US involvement in the MID began/ended
gen StrtDayInTrm1 = StartDateUS - TermStartDate
rename TermStartDate TermStartDate1  
rename TermEndDate TermEndDate1               

gen EndDayInTrm1 = EndDateUS - TermStartDate1
                  
gen DaysInTrm1 = LngthMIDUS
replace DaysInTrm1 = TermEndDate1 - StartDateUS + 1 if Pres2!=""
order StrtDayInTrm1 EndDayInTrm1

** Indicator for the US being Originator of the dispute
gen US_Init = orig

** Indicator for US being the only nation on its side of the MID        
gen US_alone = 0
replace US_alone = 1 if sidea==1 & numa==1
replace US_alone = 1 if sidea==0 & numb==1

** Indicator for US's opponent being the only nation on its side
gen opp_alone = 0
replace opp_alone = 1 if sidea==1 & numb==1
replace opp_alone = 1 if sidea==0 & numa==1

** Indicator for both parties being the only nation on each's side
gen BothAlone = US_alone*opp_alone

** Index observations within each president, ordered by the date of US
** involvement, dispute number
sort Pres1 StartDateUS dispnum3 dispnum4
by Pres1: egen int PresOb = seq()
sort StartDateUS dispnum3 dispnum4

** Indicator for fishing disputes, from Cohen and Weeks
gen FishDisp = 0
replace FishDisp = 1 if dispnum3==1705 | dispnum3==3222 | dispnum3==2845 | dispnum3==3244 | dispnum3==1803 | dispnum3==1805 | dispnum3==350 | dispnum3==1158 | dispnum3==601 | dispnum3==602 | dispnum3==2952 | dispnum3==2954 | dispnum3==2953 | dispnum3==2958 | dispnum3==2222 | dispnum3==2968 | dispnum3==2967 | dispnum3==3972 | dispnum3==4183 | dispnum3==4174

** Variable for number of opponents of US in MID
gen nOpps = numb if sidea==1
replace nOpps = numa if sidea==0

*** Create placeholder observations for Presidents in whose terms no
*** MIDs occurred
set obs `=_N + 1'
replace Pres1 = "Washington" in l
replace South1 = 1 in l
replace Order = 1 in l
replace TermStartDate = mdy(4, 30, 1789) in l
replace TermEndDate = mdy(3, 4, 1797) in l
replace TermLength = TermEndDate - TermStartDate + 1 in l
set obs `=_N + 1'
replace Pres1 = "AdamsJ" in l
replace South1 = 0 in l
replace Order = 2 in l
replace TermStartDate = mdy(3, 4, 1797) in l
replace TermEndDate = mdy(3, 4, 1801) in l 
replace TermLength = TermEndDate - TermStartDate + 1 in l
set obs `=_N + 1'
replace Pres1 = "Jefferson" in l
replace South1 = 1 in l
replace Order = 3 in l
replace TermStartDate = mdy(3, 4, 1801) in l
replace TermEndDate = mdy(3, 4, 1809) in l
replace TermLength = TermEndDate - TermStartDate + 1 in l 
set obs `=_N + 1'
replace Pres1 = "HarrisonWH" in l
replace South1 = 1 in l
replace Order = 9 in l
replace TermStartDate = mdy(3, 4, 1841) in l
replace TermEndDate = mdy(4, 6, 1841) in l 
replace TermLength = TermEndDate - TermStartDate + 1 in l
replace Party = "Whig" in l
set obs `=_N + 1'
replace Pres1 = "Taylor" in l
replace South1 = 1 in l
replace Order = 12 in l
replace TermStartDate = mdy(3, 5, 1849) in l
replace TermEndDate = mdy(7, 10, 1850) in l
replace TermLength = TermEndDate - TermStartDate + 1 in l
replace Party = "Whig" in l
set obs `=_N + 1'
replace Pres1 = "Garfield" in l
replace South1 = 0 in l
replace Order = 20 in l
replace TermStartDate = mdy(3, 4, 1881) in l
replace TermEndDate = mdy(9, 20, 1881) in l
replace TermLength = TermEndDate - TermStartDate + 1 in l
replace Party = "Republican" in l

sort TermStartDate StartDateUS dispnum3 dispnum4

** Create simpler version of party for first president
gen Party1 = Party    
replace Party1 = "Democratic" if Party=="Democratic; National Union"
replace Party1 = "Republican" if Party=="Republican; National Union"
replace Party1 = "Whig" if Party=="Whig; None"
replace Party1 = "Democratic-Republican" if  Party=="Democratic-Republican; National Republican"
encode Party1, generate (Party1Factor)

** Labels for `outcome'
label define outcome -1 "US Lose" 0 "Stalemate, Compromise, Released, Unclear, Join war, Missing" 1 "US Win"

label value outcome outcome


** Indicator for whether the US was an Originator in the dispute
gen Primary = 0
replace Primary = 1 if orig==1

** Create label for `Order' that includes missing presidents
label define PresOrder 1 "Washington" 2 "AdamsJ" 3 "Jefferson" 4 "Madison" 5 "Monroe" 6 "AdamsJQ" 7 "Jackson" 8 "Van_Buren" 9 "HarrisonWH" 10 "Tyler" 11 "Polk" 12 "Taylor" 13 "Fillmore" 14 "Pierce" 15 "Buchanan" 16 "Lincoln" 17 "JohnsonA" 18 "Grant" 19 "Hayes" 20 "Garfield" 21 "Arthur" 22 "Cleveland" 23 "HarrisonB" 24 "Cleveland" 25 "McKinley" 26 "RooseveltT" 27 "Taft" 28 "Wilson" 29 "Harding" 30 "Coolidge" 31 "Hoover" 32 "RooseveltFD" 33 "Truman" 34 "Eisenhower" 35 "Kennedy" 36 "JohnsonLB" 37 "Nixon" 38 "Ford" 39 "Carter" 40 "Reagan" 41 "BushGHW" 42 "Clinton" 43 "BushGW" 44 "Obama"

gen Pres1Order = Order
drop Order
label values Pres1Order PresOrder 

** Create cluster variable `Leader' that treats Cleveland's two terms 
** as one cluster
generate Leader = Pres1Order
replace Leader = 22 if Leader==24

** Create label for `Leader' that includes missing presidents, but 
** Cleveland is only listed once.
label define LeaderOrder 1 "Washington" 2 "AdamsJ" 3 "Jefferson" 4 "Madison" 5 "Monroe" 6 "AdamsJQ" 7 "Jackson" 8 "Van_Buren" 9 "HarrisonWH" 10 "Tyler" 11 "Polk" 12 "Taylor" 13 "Fillmore" 14 "Pierce" 15 "Buchanan" 16 "Lincoln" 17 "JohnsonA" 18 "Grant" 19 "Hayes" 20 "Garfield" 21 "Arthur" 22 "Cleveland" 23 "HarrisonB" 25 "McKinley" 26 "RooseveltT" 27 "Taft" 28 "Wilson" 29 "Harding" 30 "Coolidge" 31 "Hoover" 32 "RooseveltFD" 33 "Truman" 34 "Eisenhower" 35 "Kennedy" 36 "JohnsonLB" 37 "Nixon" 38 "Ford" 39 "Carter" 40 "Reagan" 41 "BushGHW" 42 "Clinton" 43 "BushGW" 44 "Obama"

label values Leader LeaderOrder 

generate Leader2 = .
replace Leader2 = Leader[_n] + 1 if Pres2!=""
label values Leader2 LeaderOrder

** Note: the only difference between `Leader' and `Pres1Order' is that 
** the two non-consecutive administrations of Grover Cleveland are 
** listed under the same number (22) in `Leader' but as different 
** numbers (22 and 24) in `Pres1Order'. 
 
** Previous and next presidents
gen PresPrev = Pres1Order - 1
label values PresPrev PresOrder
gen PresNext = Pres1Order + 1
label values PresNext PresOrder
replace PresPrev = . if PresPrev==0
replace PresNext = . if PresPrev==45

sort Pres1Order StartDateUS dispnum3 dispnum4

sort dispnum3 dispnum4
by dispnum3 dispnum4: egen ObInDispute = seq()
sort TermStartDate StartDateUS dispnum3 dispnum4

sort Leader StartDateUS dispnum3 dispnum4
by Leader: egen int LeaderOb = seq()

** Expressing Term Length in Years
gen TermYrsAdj = TermLeng/365.25


** Number of MIDs of each Leader in each category
by Leader, sort: egen nMIDs = total(FishDisp==0 | FishDisp==1)
by Leader, sort: egen nMIDsNoF = total(FishDisp==0)
by Leader, sort: egen nMIDsPrim = total(FishDisp==0 & Primary==1) 
by Leader, sort: egen nMIDsAlone = total(FishDisp==0 & BothAlone==1)
by Leader, sort: egen nMIDsAloneTarg = total(FishDisp==0 & BothAlone==1 & sidea==0)
by Leader, sort: egen nMIDsAloneInit = total(FishDisp==0 & BothAlone==1 & sidea==1)

** Each Leader's number of MIDs per year
gen nMIDsPerYr = nMIDs/TermYrsAdj
gen nMIDsNoFPerYr = nMIDsNoF/TermYrsAdj
gen nMIDsPrimPerYr = nMIDsPrim/TermYrsAdj
gen nMIDsAlonePerYr = nMIDsAlone/TermYrsAdj
gen nMIDsAloneTargPerYr = nMIDsAloneTarg/TermYrsAdj
gen nMIDsAloneInitPerYr = nMIDsAloneInit/TermYrsAdj

** Each leader's # of MIDs in each category in which the US used Force
by Leader, sort: egen nForce = total(ForceUS==1)
by Leader, sort: egen nForceNoF = total(ForceUS==1 & FishDisp==0)
by Leader, sort: egen nForcePrim = total(ForceUS==1 & FishDisp==0 & Primary==1) 
by Leader, sort: egen nForceAlone = total(ForceUS==1 & FishDisp==0 & BothAlone==1)

** Each leader's uses of force per year in each category
gen nForcePerYr = nForce/TermYrsAdj
gen nForceNoFPerYr = nForceNoF/TermYrsAdj
gen nForcePrimPerYr = nForcePrim/TermYrsAdj
gen nForceAlonePerYr = nForceAlone/TermYrsAdj 

** Each leader's uses of force per MID in each category
gen nForcePerMID = nForce/nMIDs
gen nForcePerMIDNoF = nForceNoF/nMIDsNoF
gen nForcePerMIDPrim = nForcePrim/nMIDsPrim
gen nForcePerMIDAlone = nForceAlone/nMIDsAlone


** Time variables
gen TermStartDateSq = TermStartDate1^2
gen TermStartDateCu = TermStartDate1^3
gen Era = 1
replace Era = 2 if Leader>=25 & Leader<33
replace Era = 3 if Leader>=33
label define Era 1 "pre-1897" 2 "1897-1945" 3 "post-1945"
label values Era Era

** Merge country-year data (this is a subset of Gelpi and 
** Feaver's data with one observation per year)
** Merging on MID Start Year
gen year = styear
sort year
merge m:1 year using "1-Original-Data/bigstickcountry-year.dta"
tab _merge
sum year if _merge==1
** G&F's data doesn't go beyond 1992.

** _merge==2 are country-years when there was not a MID.
drop if _merge==2
drop _merge


sort Pres1Order StartDateUS

replace ObInDispute = . if Leader<=3 | Leader>44
replace nMIDs = . if Leader<=3 | Leader>44
replace nMIDsNoF = . if Leader<=3 | Leader>44
replace nMIDsPrim = . if Leader<=3 | Leader>44
replace nMIDsAlone = . if Leader<=3 | Leader>44
replace nForce = . if Leader<=3 | Leader>44
replace nForceNoF = . if Leader<=3 | Leader>44
replace nForcePrim = . if Leader<=3 | Leader>44
replace nForceAlone = . if Leader<=3 | Leader>44

*** Alternative assignment of MIDs to Leader2
generate alt = .
replace alt = 0 if dispnum3!=.
** Mexican-American War (Tyler and Polk)
replace alt = 1 if dispnum3==1552
** 1958-61 dipute with North Korea (Eisenhower and Kennedy)
replace alt = 1 if dispnum3==2187
** Bay of Pigs Invasion (Eisenhower and Kennedy)
replace alt = 1 if dispnum3==40
** 1963-64 dispute with North Korea (Kennedy and JohnsonLB)
replace alt = 1 if dispnum3==2189
** Cambodian Campaign (JohnsonLB and Nixon)
replace alt = 1 if dispnum3==1806
** Invasion of Panama (Reagan and BushGHW)                         
replace alt = 1 if dispnum3==3901
** Yugoslav wars (BushGHW and Clinton)
replace alt = 1 if dispnum3==3551

generate Leader_alt = Leader
replace Leader_alt = Leader2 if alt==1
label values Leader_alt LeaderOrder

** Number of MIDs of each Leader_alt in each category
by Leader_alt, sort: egen nMIDs_alt = total(FishDisp==0 | FishDisp==1)
by Leader_alt, sort: egen nMIDsNoF_alt = total(FishDisp==0)
by Leader_alt, sort: egen nMIDsPrim_alt = total(FishDisp==0 & Primary==1) 
by Leader_alt, sort: egen nMIDsAlone_alt = total(FishDisp==0 & BothAlone==1)

** Each Leader_alt's number of MIDs per year
gen nMIDsPerYr_alt = nMIDs_alt/TermYrsAdj
gen nMIDsNoFPerYr_alt = nMIDsNoF_alt/TermYrsAdj
gen nMIDsPrimPerYr_alt = nMIDsPrim_alt/TermYrsAdj
gen nMIDsAlonePerYr_alt = nMIDsAlone_alt/TermYrsAdj

** Each leader's # of MIDs in each category in which the US used Force
by Leader_alt, sort: egen nForce_alt = total(ForceUS==1)
by Leader_alt, sort: egen nForceNoF_alt = total(ForceUS==1 & FishDisp==0)
by Leader_alt, sort: egen nForcePrim_alt = total(ForceUS==1 & FishDisp==0 & Primary==1) 
by Leader_alt, sort: egen nForceAlone_alt = total(ForceUS==1 & FishDisp==0 & BothAlone==1)

** Each leader's uses of force per year in each category
gen nForcePerYr_alt = nForce_alt/TermYrsAdj
gen nForceNoFPerYr_alt = nForceNoF_alt/TermYrsAdj
gen nForcePrimPerYr_alt = nForcePrim_alt/TermYrsAdj
gen nForceAlonePerYr_alt = nForceAlone_alt/TermYrsAdj 

** Each leader's uses of force per MID in each category
gen nForcePerMID_alt = nForce_alt/nMIDs
gen nForcePerMIDNoF_alt = nForceNoF_alt/nMIDsNoF
gen nForcePerMIDPrim_alt = nForcePrim_alt/nMIDsPrim
gen nForcePerMIDAlone_alt = nForceAlone_alt/nMIDsAlone

order Leader Leader_alt StartDateUS EndDateUS South1 outcome fatality ForceUS LngthMIDUS dispnum3 dispnum4 Party MiltExp MiltOcc TermLength proprec Era lnprevdead pctvetmi

sort Pres1Order StartDateUS
 
saveold "3-Analysis-Data/15-03-13-USMIDs2.dta", replace
outsheet using 3-Analysis-Data/15-03-13-USMIDs2.csv , comma replace
